//
//  NSDictionary+DZCategory.m
//
//
//  Created by Darren on 15/6/16.
//
//

#import "NSDictionary+DZCategory.h"
#import "NSObject+DZCategory.h"
// helper function: get the string form of any object
static NSString *toString(id object) {
    return [NSString stringWithFormat: @"%@", object];
}

// helper function: get the url encoded string form of any object
static NSString *urlEncode(id object) {
    NSString *string = toString(object);
    return [string stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
}
@implementation NSDictionary (DZCategory)

-(NSString*) urlEncodedString {
    NSMutableArray *parts = [NSMutableArray array];
    for (id key in self) {
        id value = [self objectForKey: key];
        NSString *part = [NSString stringWithFormat: @"%@=%@", urlEncode(key), urlEncode(value)];
        [parts addObject: part];
    }
    return [parts componentsJoinedByString: @"&"];
}


- (NSString *)dz_stringForKey:(NSString *)strKey
{
    if ([self objectForKey:strKey] && [[self objectForKey:strKey] isKindOfClass:[NSString class]])
    {
        return [self objectForKey:strKey];
    }
    else
    {
        return nil;
    }
}

- (NSArray *)dz_arrayForKey:(NSString *)strKey
{
    if ([self objectForKey:strKey] && [[self objectForKey:strKey] isKindOfClass:[NSArray class]])
    {
        return [self objectForKey:strKey];
    }
    else
    {
        return nil;
    }
}

- (NSDictionary *)dz_dictForKey:(NSString *)strKey
{
    if ([self objectForKey:strKey] && [[self objectForKey:strKey] isKindOfClass:[NSDictionary class]])
    {
        return [self objectForKey:strKey];
    }
    else
    {
        return nil;
    }
}

- (NSMutableArray *)dz_modelsForArrayKey:(NSString *)strKey
                          modelClassName:(NSString *)model
                                  except:(NSArray *)except
                                   block:(void (^)(NSDictionary *dictInner, id model))block
{
    NSMutableArray *mArray = [NSMutableArray new];
    for (NSDictionary *dict in [self dz_arrayForKey:strKey])
    {
        Class cl = NSClassFromString(model);
        id m = [cl new];
        [m dz_modelWithDictionary:dict except:except];
        if (block)
        {
            block(dict, m);
        }
        if (m)
        {
            [mArray addObject:m];
        }
    }
    return mArray;
}

- (id)dz_modelForDictKey:(NSString *)strKey
          modelClassName:(NSString *)model
           modelInstance:(id)modelIns
                  except:(NSArray *)except
                   block:(void (^)(NSDictionary *dictInner, id model))block
{
    
    Class cl = NSClassFromString(model);
    id m = nil;
    if (modelIns)
    {
        m = modelIns;
    }
    else
    {
        m = [cl new];
    }
    [m dz_modelWithDictionary:[self dz_dictForKey:strKey] except:except];
    if (block)
    {
        block([self dz_dictForKey:strKey], m);
    }
    
    return m;
}

- (id)dz_modelForInstance:(id)instance
{
    for (NSString *key in self)
    {
        [instance dz_setValue:self[key] forKey:key];
    }
    return instance;
}

@end
